package com.adingxiong.quickstart.project.controller;


import com.adingxiong.quickstart.project.entity.Discuss;
import com.adingxiong.quickstart.project.service.IDiscussService;
import com.adingxiong.quickstart.result.Result;
import com.adingxiong.quickstart.result.RetCode;
import com.adingxiong.quickstart.util.GetRealIp;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 评论表 前端控制器
 * </p>
 *
 * @author xiongc
 * @since 2020-12-28
 */
@RestController
@RequestMapping("/discuss")
public class DiscussController {

    @Autowired
    private IDiscussService service;

    private final String suffix =  ")";

    private final String Phone = "el-icon-mobile-phone";

    private final String pc = "el-icon-monitor";



    @PostMapping("/save")
    public Result saveInfo(@RequestBody Discuss discuss , HttpServletRequest request) {
        String userAgent = request.getHeader("User-Agent");
        if(discuss == null) {
            return new Result(RetCode.PARAM_ERROR.getCode(),RetCode.PARAM_ERROR.getMsg(), null);
        }
        discuss.setCreateTime(new Date());
        if(!StringUtils.isEmpty(userAgent) && userAgent.indexOf(suffix) > 0) {
            discuss.setSurefireType(userAgent.substring(0,userAgent.indexOf(suffix) + 1));
        }
        if(StringUtils.isEmpty(discuss.getUsername())){
            discuss.setUsername("匿名用户");
        }
        boolean save = service.save(discuss);
        return new Result(save ? RetCode.SUCCESS.getCode() : RetCode.SYSTEM_ERROR.getCode(), save ? "发送成功" : "发送失败", save);
    }
    @GetMapping("/list")
    public Result findList (
            @ApiParam(value = "当前页码") @RequestParam(value = "current" ,required = false,defaultValue = "1") Integer current,
            @ApiParam(value = "每页条数") @RequestParam(value = "pageSize" ,required = false,defaultValue = "10") Integer pageSize
    ){
        QueryWrapper<Discuss> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .lambda().and(e ->e.isNull(Discuss::getPublishId))
                .orderByDesc(Discuss::getCreateTime);
        Page<Discuss> page = new Page<>(current,pageSize);
        Page<Discuss> res = service.page(page,queryWrapper);

        List<Discuss> records = res.getRecords();
        if(!CollectionUtils.isEmpty(records)){
            // 填充回复词条
            records.forEach(e -> {
                QueryWrapper<Discuss> query = new QueryWrapper<>();
                query.eq("publish_id",e.getId());
                List<Discuss> list = service.list(query);

                String surefireType = e.getSurefireType();
                if(!StringUtils.isEmpty(surefireType) && surefireType.contains("(Linux")){
                    e.setType(Phone);
                }else {
                    e.setType(pc);
                }
                e.setBackList(list);
            });
        }
        res.setRecords(records);
        return new Result(RetCode.SUCCESS.getCode(),RetCode.SUCCESS.getMsg(),res);
    }
}

